package com.miguo.framework.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.TextView;
import android.view.Window;
import android.view.WindowManager;
import android.content.DialogInterface;
import android.os.Message;
import android.app.Activity;

import com.miguo.framework.R;

public class CustomToast extends Dialog implements Runnable {
    private Context mContext;
    private Dialog mDialog;
    private int mShowTime;
    private TextView mTipsText;
    private static CustomToast mToast;

    public static CustomToast getInstance(Context context) {
        if (mToast != null) {
            mToast.dismiss();
        }
        mToast = new CustomToast(context);
        return mToast;
    }

    private CustomToast(Context context) {
        super(context, R.style.toastStyle);
        mContext = context;
        mDialog = new Dialog(mContext, R.style.toastStyle);
        initView();
        initData();
    }

    private void initView() {
        mDialog.setContentView(R.layout.custom_toast_two);
        mTipsText = (TextView) mDialog.findViewById(R.id.toast_tv);
    }

    private void initData() {
        Window dialogWindow = mDialog.getWindow();
        WindowManager.LayoutParams winLayoutParams = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setAttributes(winLayoutParams);
        mDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (event.getAction() == 0) {
                    switch (keyCode) {
                        case 0:
                            CustomToast.this.dismiss();
                            break;
                    }
                }
                return false;
            }
        });
    }

    public void run() {
        try {
            switch (this.mShowTime) {
                case 0:
                    Thread.sleep(this.mShowTime);
                    sendMessage(0);
                    break;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void sendMessage(int msg) {
        Message message = new Message();
        message.what = msg;
        mHandler.sendMessage(message);
    }

    private Handler mHandler = new Handler() {

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    dismiss();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    public void setMessage(int msg) {
        mTipsText.setText(msg);
    }

    public void setMessage(CharSequence msg) {
        mTipsText.setText(msg);
    }

    public void showTime(int time) {
        mShowTime = time;
    }

    public void show() {
        if ((this.mContext != null) && (!((Activity) this.mContext).isFinishing())) {
            this.mDialog.show();
            new Thread(this).start();
        }
    }

    public void dismiss() {
        if (mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }
}

